<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Models\Article;
use App\Http\Requests\AddArticleRequest;
use Illuminate\Http\Request;

class ArticleController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $datas = Article::all();
        return view("admin.article.index",compact('datas'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("admin.article.add");
    }

    /**
     * 添加
     * @param Request $request
     */
    public function store(AddArticleRequest $request)
    {


        //文件上传(封面图片)
        if($request->hasFile('pic')){
            $ret = $request->file('pic')->store('','article');  // *.jpg
            $pic = '/uploads/article'.$ret;
        }

        $post = $request->except('_token');
        $post['pic'] = $pic??"";
        //添加到数据库
        Article::create($post);
        return redirect(route('admin.article.add'));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Http\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function show(Article $article)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Http\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function edit(int $id)
    {
        $article = Article::find($id);
        return view('admin.article.edit',compact('article'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Http\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Article $article)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Http\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function destroy(Article $article)
    {
        $article->delete();
        return ['id'=>1];
    }
}
